package com.aismy.admin.controller;

import com.aismy.admin.dto.CommentDto;
import com.aismy.admin.service.ArticleCommentService;
import com.aismy.admin.utils.SecurityUtils;
import com.aismy.admin.vo.CommentVO;
import com.aismy.admin.vo.Result;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/article")
@RequiredArgsConstructor
public class ArticleCommentController {

    private final ArticleCommentService commentService;

    @PostMapping("/{articleId}/comment")
    public Result<Void> addComment(
            @PathVariable Long articleId,
            @RequestBody CommentDto request) {
        Long userId = SecurityUtils.getUserId();
        commentService.addComment(articleId, userId, request.getContent(), request.getParentId());
        return Result.success();
    }

    @DeleteMapping("/comment/{id}")
    public Result<Void> deleteComment(@PathVariable Long id) {
        Long userId = SecurityUtils.getUserId();
        commentService.deleteComment(id, userId);
        return Result.success();
    }

    @GetMapping("/{articleId}/comments")
    public Result<Page<CommentVO>> getArticleComments(
            @PathVariable Long articleId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        return Result.success(commentService.getArticleComments(articleId, page, size));
    }

    @GetMapping("/comment/{commentId}/replies")
    public Result<Page<CommentVO>> getCommentReplies(
            @PathVariable Long commentId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        return Result.success(commentService.getCommentReplies(commentId, page, size));
    }
}
